$(function(){
    const $name = $("input:eq(0)");  // 验证 查重
    const $passwd = $("input:eq(1)");// 验证
    const $email = $("input:eq(2)"); // 验证
    const $phone = $("input:eq(3)"); // 验证 查重
    const $labelName = $("td:eq(0)");
    const $labelPasswd = $("#passwd");
    const $labelEmail = $("td:eq(4)");
    const $labelPhone = $("td:eq(6)");
    const $input = $("input");
    {
        $name.bind('input propertychange', function () {
            test_ipt(testName, $name, $labelName);})
        $passwd.bind('input propertychange', function () {
            test_ipt(testPasswd, $passwd, $labelPasswd);})
        $email.bind('input propertychange', function () {
            test_ipt(testEmail, $email, $labelEmail);})
        $phone.bind('input propertychange', function () {
            test_ipt(testPhone, $phone, $labelPhone);})
    }
    showTips($passwd, $labelPasswd);
    const submit = function(){
        $name.val($name.val().trim());
        $email.val($email.val().trim());
        $phone.val($phone.val().trim());
        if (!test_ipt(testName, $name, $labelName)){
            layer.msg('昵称格式有误');
            $name.focus();
            return false;
        }
        if (!test_ipt(testPasswd,$passwd,$labelPasswd)){
            layer.msg('密码格式有误');
            $passwd.focus();
            return false;
        }
        if (!test_ipt(testEmail,$email,$labelEmail)){
            layer.msg('电子邮箱格式有误');
            $email.focus();
            return false;
        }
        if (!test_ipt(testPhone,$phone,$labelPhone)){
            layer.msg('请输入正确的手机号码');
            $phone.focus();
            return false;
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/user/add.do", {
            name:$name.val(),
            passwd:$passwd.val(),
            email:$email.val(),
            phone:$phone.val(),
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status > 0) {
                $name.val("");
                $passwd.val("");
                $email.val("");
                $phone.val("");
            } else {
                switch (data.status) {
                    case -2:
                        $name.addClass("input-error");
                        $labelName.addClass("input-error");
                        $name.focus();
                        break;
                    case -3:
                        $phone.addClass("input-error");
                        $labelPhone.addClass("input-error");
                        $phone.focus();
                        break;
                    case -404:
                        location.assign('/login.html');
                }
            }
        });
        return false;
    };
    setEnterKeyEvent($input, submit);
    $("#submit").click(submit);
    $("#reset").click(function(){
        $input.val("");
    })
});